package com.blog.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.common.utils.WebUtils;
import com.blog.controller.base.BaseController;
import com.blog.model.entity.Article;
import com.blog.model.entity.Label;
import com.blog.model.vo.ArticleVO;
import com.blog.model.vo.R;
import com.blog.service.ArticleService;
import com.blog.service.LabelService;
import com.vividsolutions.jts.operation.valid.IsValidOp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 首页控制器
 *
 * @author avalon
 * @date 2019/4/19
 */
@RequestMapping(value = "/home")
@Controller
@Slf4j
public class HomeController extends BaseController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private LabelService labelService;

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/")
    public ModelAndView homePre() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("front/home/home");
        IPage<ArticleVO> pages = articleService.pageFrontArticleVO(new Page<ArticleVO>(1, 10), null);
        modelAndView.addObject("pages", R.page(pages));
        return modelAndView;
    }

    /**
     * 翻页请求文章
     *
     * @param page
     * @param size
     * @param title
     * @return
     */
    @RequestMapping("/article")
    public ModelAndView pageArticle(@RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "10") int size, @RequestParam(value = "title", defaultValue = "") String title) {
        QueryWrapper<ArticleVO> articleVOQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(title)) {
            articleVOQueryWrapper.lambda().like(ArticleVO::getTitle, title);
            articleVOQueryWrapper.lambda().orderByDesc(ArticleVO::getCreateDate);
        }
        IPage<ArticleVO> pages =
            articleService.pageFrontArticleVO(new Page<ArticleVO>(page, size), articleVOQueryWrapper);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("front/home/article");
        modelAndView.addObject("pages", R.page(pages));
        modelAndView.addObject("currPage", page);

        return modelAndView;
    }

    /**
     * 排行榜
     *
     * @return
     */
    @RequestMapping("/isView")
    public ModelAndView isView() {
        QueryWrapper<ArticleVO> articleVOQueryWrapper = new QueryWrapper<>();
        articleVOQueryWrapper.lambda().orderByDesc(ArticleVO::getViewCount);
        IPage<ArticleVO> pages = articleService.pageFrontArticleVO(new Page<ArticleVO>(1, 5), articleVOQueryWrapper);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("front/home/isView");
        modelAndView.addObject("pages", R.page(pages));
        modelAndView.addObject("one", pages.getRecords().size() == 0 ? null : pages.getRecords().get(0));

        return modelAndView;
    }

    /**
     * 排行榜
     *
     * @return
     */
    @RequestMapping("/isUp")
    public ModelAndView isUp() {
        QueryWrapper<ArticleVO> articleVOQueryWrapper = new QueryWrapper<>();
        articleVOQueryWrapper.lambda().orderByDesc(ArticleVO::getUpCount);
        IPage<ArticleVO> pages = articleService.pageFrontArticleVO(new Page<ArticleVO>(1, 5), articleVOQueryWrapper);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("front/home/isUp");
        modelAndView.addObject("pages", R.page(pages));
        modelAndView.addObject("one", pages.getRecords().size() == 0 ? null : pages.getRecords().get(0));

        return modelAndView;
    }

    /**
     * 标签云
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/labels")
    public ModelAndView pageLabels(@RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "10") int size) {
        List<Label> list = labelService.list();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("front/home/labels");
        modelAndView.addObject("labels", list);
        return modelAndView;
    }


    @RequestMapping("/time")
    public ModelAndView time(){
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<ArticleVO> articleVOQueryWrapper = new QueryWrapper<>();
        IPage<ArticleVO> pages = articleService.pageFrontArticleVO(new Page<ArticleVO>(1, 99), articleVOQueryWrapper);
        modelAndView.setViewName("front/home/time");
        modelAndView.addObject("pages", R.page(pages));
        return modelAndView;
    }


    @RequestMapping("/detail")
    public ModelAndView detail(@RequestParam(defaultValue = "0") Long id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("article", articleService.findById(id));
        modelAndView.setViewName("front/home/detail");
        return modelAndView;
    }

}
